package com.woniu.p2p.behindController;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.BehindCouponService;
import com.woniu.p2p.entity.Coupon;
import com.woniu.p2p.listener.CouponListener;
import com.woniu.p2p.vo.ImportCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 86152
 * @create 2020/8/12
 * @since 1.0.0
 */
@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    BehindCouponService behindCouponService;
    /*条件查询*/
    @RequestMapping("/getcouponbycons")
    public String getcouponbycons(Map maps,String status, String keyword, Integer currentPage, @DateTimeFormat(pattern = "yyyy-MM-dd")Date starttime,@DateTimeFormat(pattern = "yyyy-MM-dd")Date endtime){
        currentPage=currentPage==null?1:currentPage;
        int pagesize=4;
        PageInfo<Coupon> couponPageInfo=behindCouponService.getcouponbycons(status,keyword,starttime,endtime,currentPage,pagesize);
        maps.put("coupons",couponPageInfo);
        maps.put("keyword",keyword);
        maps.put("status",status);
        maps.put("starttime",starttime);
        maps.put("endtime",endtime);
        /*查询并返回所有的优惠券id用于反选*/
        List<Coupon> list = behindCouponService.selectAll();
        ArrayList<Integer> ids = new ArrayList<>();
        for(Coupon coupon :list){
            ids.add(coupon.getCouponId());
        }
        maps.put("ids",ids);
        return "content/discountManager";
    }
    /*根据id删除优惠券*/
    @RequestMapping("/deletebyid")
    public String deletebyid(Map maps,String status, String keyword, Integer currentPage, @DateTimeFormat(pattern = "yyyy-MM-dd")Date starttime,@DateTimeFormat(pattern = "yyyy-MM-dd")Date endtime,Integer couponId){
        currentPage=currentPage==null?1:currentPage;
        int pagesize=4;
        PageInfo<Coupon> couponPageInfo=behindCouponService.deletebyid(status,keyword,starttime,endtime,currentPage,pagesize,couponId);
        maps.put("coupons",couponPageInfo);
        maps.put("keyword",keyword);
        maps.put("status",status);
        maps.put("starttime",starttime);
        maps.put("endtime",endtime);
        return "content/discountManager";
    }
    /*根据id查询优惠券*/
    @RequestMapping("/getcoupondetailsbyid")
    @ResponseBody
    public Coupon getcoupondetailsbyid(Map maps,Integer couponId){
        Coupon coupon=behindCouponService.findcouponbyid(couponId);
        return coupon;
    }
    /*添加优惠券*/
    @RequestMapping("/addcoupon")
    @ResponseBody
    public Integer addcoupon(String couponName,String couponContext,Long couponMoney,@DateTimeFormat(pattern = "yyyy-MM-dd")Date couponTime,String couponCondition,String couponStatus){
        Coupon coupon = new Coupon();
        coupon.setCouponName(couponName);
        coupon.setCouponContext(couponContext);
        coupon.setCouponMoney(couponMoney);
        coupon.setCouponTime(couponTime);
        coupon.setCouponCondition(couponCondition);
        coupon.setCouponStatus(couponStatus);
        return behindCouponService.addcoupon(coupon);
    }
    /*批量删除优惠券*/
    @RequestMapping("/piliangdel")
    @ResponseBody
    public Integer piliangdel(int[] ids){
        return behindCouponService.piliangdel(ids);
    }

    /*导出*/
    @RequestMapping("/exportcoupon")
    @ResponseBody
    public void exportcoupon(int[] ids, HttpServletResponse response) throws IOException {
        ServletOutputStream outputStream = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("测试","UTF-8");
        response.setHeader("Content-Dispostion","attachment;filename*=UTF-8''"+fileName+".xlsx");
        List<Coupon> coupons = new ArrayList<>();
        for(int id:ids){
            Coupon findcouponbyid = behindCouponService.findcouponbyid(id);
            coupons.add(findcouponbyid);
        }
        ExcelWriterBuilder write = EasyExcel.write(outputStream, Coupon.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        sheet.doWrite(coupons);
    }
    @Autowired
    CouponListener couponListener;
    /*导入*/
    @RequestMapping("/importcoupon")
    @ResponseBody
    public Integer importcoupon(@RequestParam("file")MultipartFile file){
        try {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), ImportCouponVo.class, couponListener);
            ExcelReaderSheetBuilder sheet = read.sheet();
            sheet.doRead();

        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }
}
